package com.dearbinge.opentapi.content.comment;


import com.baomidou.mybatisplus.plugins.Page;
import com.dearbinge.data.api.comment.MessageCommentService;
import com.dearbinge.data.pojo.comment.MessageComment;
import com.dearbinge.data.pojo.comment.Reply;
import com.dearbinge.data.pojo.comment.vo.CommentReplyVO;
import com.dearbinge.data.pojo.comment.vo.CommentVO;
import com.dearbinge.data.pojo.comment.vo.ReplyVO;
import com.dearbinge.utils.ResultMsg;
import com.dearbinge.utils.enums.ResultStatusCodeEnum;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

import java.util.List;

/**
 * 评论控制层
 *
 * @author wes
 * @date 18:22 2019/1/4
 */
@Api(tags = "4、秀场-评论信息")
@RestController
@RequestMapping("/comment")
@EnableAutoConfiguration
public class MessageCommentController extends WebMvcConfigurerAdapter {
    @Autowired
    private MessageCommentService messageCommentService;


    
    /**
     * 根据Id和分类查询评论详情
     */
    @ApiOperation(value = "根据Id和分类查询评论详情", notes = "根据Id和分类查询评论详情")
    @RequestMapping(value = "/getCommentReply", method = RequestMethod.GET)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "commentType",value = "类型（1：生活  2：商品  3：秀场）",paramType="query"),
            @ApiImplicitParam(name = "userId",value = "用户id",paramType="query"),
            @ApiImplicitParam(name = "entityId",value = "实体id 对应信息表的id",paramType="query")})
    @ApiResponses({
            @ApiResponse(response = CommentReplyVO.class, code = 200, message = "返回结果参数")})
    public ResultMsg<List<Object>> getCommentReply(Integer commentType, String entityId, String userId) {
        try {
            List<Object> result = messageCommentService.getCommentReply(commentType, entityId, userId);
            return ResultMsg.ok(result);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResultMsg.error(ResultStatusCodeEnum.CODE500.getCode(), ResultStatusCodeEnum.CODE500.getDesc());
    }


    /**
     * 分页获取评论和回复集
     */
    @ApiOperation(value = "分页获取评论和回复集", notes = "分页获取评论和回复集")
    @RequestMapping(value = "/listCommentReply", method = RequestMethod.GET)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "commentType", value = "类型（1：生活  2：商品  3：秀场）", paramType = "query", required = true),
            @ApiImplicitParam(name = "userId", value = "用户id", paramType = "query", required = true),
            @ApiImplicitParam(name = "entityId", value = "实体id 对应信息表的id", paramType = "query", required = true),
            @ApiImplicitParam(name = "lastId", value = "第一次分页的最新的id", paramType = "query"),
            @ApiImplicitParam(name = "current", value = "当前页", paramType = "query"),
            @ApiImplicitParam(name = "size", value = "页面记录数", paramType = "query")
    })
    @ApiResponses({
            @ApiResponse(response = CommentVO.class, code = 200, message = "返回结果参数"),
            @ApiResponse(response = ReplyVO.class, code = 200, message = "返回结果参数")
    })
    public ResultMsg<Page> listCommentReply(Integer commentType, String entityId, String userId, Integer lastId, Integer current, Integer size) {
        try {
            Page result = messageCommentService.listCommentReply(commentType, entityId, userId, lastId, current, size);
            return ResultMsg.okData(result, ResultStatusCodeEnum.SUCCESS.getDesc());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResultMsg.error(ResultStatusCodeEnum.CODE500.getCode(), ResultStatusCodeEnum.CODE500.getDesc());
    }


    /**
     * 分页获取回复集
     */
    @ApiOperation(value = "分页获取回复集", notes = "分页获取回复集")
    @RequestMapping(value = "/listReplyPage", method = RequestMethod.GET)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "commentId", value = "评论id", paramType = "query", required = true),
            @ApiImplicitParam(name = "lastId", value = "首次分页返回的最大id", paramType = "query"),
            @ApiImplicitParam(name = "current", value = "当前页", paramType = "query"),
            @ApiImplicitParam(name = "size", value = "页面记录数", paramType = "query")
    })
    @ApiResponses({
            @ApiResponse(response = ReplyVO.class, code = 200, message = "返回结果参数")
    })
    public ResultMsg<Page> listReplyPage(String commentId, Integer lastId, Integer current, Integer size) {
        try {
            Page result = messageCommentService.listReplyPage(commentId, lastId, current, size);
            return ResultMsg.okData(result, ResultStatusCodeEnum.SUCCESS.getDesc());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResultMsg.error(ResultStatusCodeEnum.CODE500.getCode(), ResultStatusCodeEnum.CODE500.getDesc());
    }



    /**
     * 增加修改回复
     */
    @ApiOperation(value = "增加修改回复", notes = "增加修改回复")
    @RequestMapping(value = "/addOrUpdateReply", method = RequestMethod.POST)
    public ResultMsg<String> addOrUpdateReply(@RequestBody Reply reply) {
        try {
            Integer dataCode ;
            if (reply.getReplyId() == null || reply.getReplyId() == 0){
                dataCode = messageCommentService.insertReply(reply);
                if (dataCode >0){
                    return ResultMsg.ok("增加成功");
                }
            } else {
                dataCode = messageCommentService.updateReply(reply);
                if (dataCode >0){
                    return ResultMsg.ok("修改成功");
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResultMsg.error(ResultStatusCodeEnum.CODE500.getCode(), ResultStatusCodeEnum.CODE500.getDesc());
    }

    /**
     * 增加修改评论
     */
    @ApiOperation(value = "增加修改评论", notes = "增加修改评论")
    @RequestMapping(value = "/addOrUpdateMessageComment", method = RequestMethod.POST)
    public ResultMsg<String> addOrUpdateMessageComment( @RequestBody MessageComment messageComment) {
        try {
            Integer dataCode ;
            if(messageComment.getCommentId() == null || messageComment.getCommentId() ==0 ){
                dataCode = messageCommentService.insertMessageComment(messageComment);
                if (dataCode >0){
                    return ResultMsg.ok("增加成功");
                }
            }else {
                dataCode = messageCommentService.updateMessageComment(messageComment);
                if (dataCode >0){
                    return ResultMsg.ok("修改成功");
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResultMsg.error(ResultStatusCodeEnum.CODE500.getCode(), ResultStatusCodeEnum.CODE500.getDesc());
    }

    /**
     * 根据id删除评论
     */
    @ApiOperation(value = "删除评论", notes = "删除评论")
    @RequestMapping(value = "/deleteMessageComment", method = RequestMethod.GET)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "commentId", value = "评论表ID", paramType = "query", required = true),
            @ApiImplicitParam(name = "tableId", value = "表ID（不用类型的表的主ID）", paramType = "query", required = true),
            @ApiImplicitParam(name = "tableType", value = "对应的表类型（1：生活  2：商品  3：秀场）", paramType = "query", required = true)
    })
    public ResultMsg<String> deleteMessageComment(Integer commentId, Integer tableType, Integer tableId) {
        try {
            Integer dataCode = messageCommentService.deleteMessageComment(commentId, tableType, tableId);
            if (dataCode >0){
                return ResultMsg.ok("删除成功");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResultMsg.error(ResultStatusCodeEnum.CODE500.getCode(), ResultStatusCodeEnum.CODE500.getDesc());
    }


    /**
     * 删除回复
     */
    @ApiOperation(value = "删除回复", notes = "删除回复")
    @RequestMapping(value = "/deleteReply", method = RequestMethod.GET)
    @ApiImplicitParam(name = "id",value = "主键id",paramType="query")
    public ResultMsg<String> deleteReply(Integer id) {
        try {
            Integer result = messageCommentService.deleteReply(id);
            if (result >0){
                 return ResultMsg.ok("ok");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResultMsg.error(500,"系统异常");
    }


}
